//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

package org.prx.prp.view;

import java.io.IOException;

import org.prx.prp.IRockOnNextGenService;
import org.prx.prp.R;
import org.prx.prp.BackgroundPlaybackService;
import org.prx.prp.controller.ImageCacheManager;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class EpisodeDetailsView extends PRXCustomActivity {
	private Bundle bundle;
	private Intent intent;
	private ServiceConnection mServiceConnection = new ServiceConnection() {
		private IRockOnNextGenService mService;

		public void onServiceConnected(ComponentName classname, IBinder obj) {
			mService = IRockOnNextGenService.Stub.asInterface(obj);
			final ImageButton playButton = (ImageButton) findViewById(R.id.episode_play_button);
			playButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {

					try {
						mService
								.openFile(bundle.getString("title"),bundle.getString("program_name"), bundle
										.getString("enclosure"), bundle
										.getInt("episode_id"));
						mService.play();
					} catch (RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		}

		public void onServiceDisconnected(ComponentName classname) {
		}

	};
	private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Toast msg = Toast.makeText(getBaseContext(), "Error in connecting to stream", Toast.LENGTH_LONG);
			msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
			msg.show();
		}
	};

	void setHeaderText(int idOfView, String headerText) {
		View headerView = findViewById(idOfView);
		TextView headerTextView = (TextView) headerView
				.findViewById(R.id.header_text);
		headerTextView.setText(headerText);
	}

	void setTextView(int idOfView, String text) {
		TextView textView = (TextView) findViewById(idOfView);
		textView.setText(text);
		textView.setVisibility(TextView.VISIBLE);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		unbindService(mServiceConnection);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			intent = new Intent(this, BackgroundPlaybackService.class);
			bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
			bundle = this.getIntent().getExtras();
			IntentFilter filter = new IntentFilter();
			filter.addAction("org.prx.errorInStream");
			this.registerReceiver(mIntentReceiver, new IntentFilter(filter));
			setContentView(R.layout.episode_details_view);
			FrameLayout frameLayout = (FrameLayout) findViewById(R.id.fm_layout);
			TextView textView = (TextView) frameLayout
					.findViewById(R.id.program_name_text_view);
			textView.setText(bundle.getString("program_name"));
			ImageView logo=(ImageView)findViewById(R.id.program_logo); 
			String imageURL=bundle.getString("PIURL");
	        if(!imageURL.equals(""))
				try {
					logo.setImageBitmap(ImageCacheManager.getInstance().getBitmapFromURL(imageURL,0));
				} catch (IOException e) {
					//TODO Auto-generated catch block
					logo.setImageResource(R.drawable.icon);				
				}        
	        
			if(bundle.getString("summary").equals(""))
				setTextView(R.id.summary_text_view,"No Description available");
			else
				setTextView(R.id.summary_text_view, bundle.getString("summary"));
			setTextView(R.id.episode_name_text_view, bundle.getString("title"));
			ImageButton imageButton=(ImageButton)findViewById(R.id.share_button);
			imageButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "PRP Episode");
                    shareIntent.putExtra(Intent.EXTRA_TEXT,bundle.getString("title")+" from "+bundle.getString("program_name"));
                    shareIntent.setType("text/plain");
                    startActivity(Intent.createChooser(shareIntent, "PRX"));
					
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
